const dayjs = require("dayjs");
// 格式化时间 YYYY/MM/DD HH:mm
function dateFormat(fmt, date) {
    return dayjs(date).format(fmt)
}

const setDateFormat = (time, format) => {
    return time ? dateFormat(format, time) : "-";
}

// 同步睡眠等待
function sleep(time, con) {
    return new Promise(resolve => {
        setTimeout(() => {
            console.log(con)
            resolve("A");
        }, time);
    });
}

function changeUndefined(postData) {
    if (postData instanceof Object) {
        for (const key in postData) {
            if (postData[key] === undefined) {
                postData[key] = ""
            }
        }
        return postData
    }
}

function updateWsData(socketData, xindianData) {
    socketData.forEach(item => {
        let findAlarmCfg = null
        let catchCfg = null
        let catchKey = -1
        let catchGroupKey = -1
        if (item.itemName == "CVPMean" || item.itemName == "T1" || item.itemName == "T2") {
            item.itemValue = parseFloat(item.itemValue).toFixed(1);
        }
        _.forEach(xindianData, (realtimeItem, realtimeItemKey) => {
            // 如果有分组则在分组里查找，并且找到分组的索引
            if (realtimeItem.groupItems) {
                let temp = _.find(realtimeItem.groupItems, { "itemName": item.itemName })
                if (temp) {
                    findAlarmCfg = temp
                    catchKey = realtimeItemKey
                    catchGroupKey = _.findIndex(realtimeItem.groupItems, { "itemName": item.itemName });
                }
                // 如果没有分组则在单个指标里找，并找到索引
            } else {
                if (realtimeItem.itemName == item.itemName) {
                    findAlarmCfg = realtimeItem
                    catchKey = realtimeItemKey
                }
            }
        })
        if (findAlarmCfg) {
            // 开始筛选命中的范围
            _.forEach(findAlarmCfg.alarmCfgs, cfgItem => {
                // 先智能报警再普通报警
                if (cfgItem.alarmLevel == "4") {
                    if (_.includes(item.itemValue, ".")) {
                        if ((parseFloat(item.itemValue) >= parseFloat(cfgItem.criticalUpper) && cfgItem.criticalUpper) || (parseFloat(item.itemValue) <= parseFloat(cfgItem.criticalLower) && cfgItem.criticalLower)) {
                            catchCfg = cfgItem
                        }
                    } else {
                        if ((item.itemValue >= cfgItem.criticalUpper && cfgItem.criticalUpper) || (item.itemValue <= cfgItem.criticalLower && cfgItem.criticalLower)) {
                            catchCfg = cfgItem
                        }
                    }
                } else if (cfgItem.alarmLevel == "5") {
                    if (_.includes(item.itemValue, ".")) {
                        if ((parseFloat(item.itemValue) >= parseFloat(cfgItem.criticalUpper) && cfgItem.criticalUpper) || (parseFloat(item.itemValue) <= parseFloat(cfgItem.criticalLower) && cfgItem.criticalLower)) {
                            catchCfg = cfgItem
                        }
                    } else {
                        if ((item.itemValue >= cfgItem.criticalUpper && cfgItem.criticalUpper) || (item.itemValue <= cfgItem.criticalLower && cfgItem.criticalLower)) {
                            catchCfg = cfgItem
                        }
                    }
                } else if (cfgItem.alarmLevel == "0") {
                    // 全都有等于
                    if (_.includes(item.itemValue, ".")) {
                        if ((parseFloat(item.itemValue) >= parseFloat(cfgItem.criticalLower) && cfgItem.criticalLower) && (parseFloat(item.itemValue) <= parseFloat(cfgItem.criticalUpper) && cfgItem.criticalUpper)) {
                            catchCfg = cfgItem
                        }
                    } else {
                        if ((item.itemValue >= cfgItem.criticalLower && cfgItem.criticalLower) && (item.itemValue <= cfgItem.criticalUpper && cfgItem.criticalUpper)) {
                            catchCfg = cfgItem
                        }
                    }
                } else if (cfgItem.alarmLevel == "1") {
                    // ＞下限小值，≤下限大值，
                    // ≥上限小值， ＜上限大值
                    if (_.includes(item.itemValue, ".")) {
                        if (cfgItem.criticalUpperObj) {
                            if ((parseFloat(item.itemValue) >= parseFloat(cfgItem.criticalUpperObj.criticalMin) && cfgItem.criticalUpperObj.criticalMin) && (parseFloat(item.itemValue) < parseFloat(cfgItem.criticalUpperObj.criticalMax) && cfgItem.criticalUpperObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalUpperObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalUpperObj.criticalMax
                            }
                            if ((parseFloat(item.itemValue) > parseFloat(cfgItem.criticalLowerObj.criticalMin) && cfgItem.criticalLowerObj.criticalMin) && (parseFloat(item.itemValue) <= parseFloat(cfgItem.criticalLowerObj.criticalMax) && cfgItem.criticalLowerObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalLowerObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalLowerObj.criticalMax
                            }
                        }
                    } else {
                        if (cfgItem.criticalUpperObj) {
                            if ((item.itemValue >= cfgItem.criticalUpperObj.criticalMin && cfgItem.criticalUpperObj.criticalMin) && (item.itemValue < cfgItem.criticalUpperObj.criticalMax && cfgItem.criticalUpperObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalUpperObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalUpperObj.criticalMax
                            }
                            if ((item.itemValue > cfgItem.criticalLowerObj.criticalMin && cfgItem.criticalLowerObj.criticalMin) && (item.itemValue <= cfgItem.criticalLowerObj.criticalMax && cfgItem.criticalLowerObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalLowerObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalLowerObj.criticalMax
                            }
                        }

                    }
                } else if (cfgItem.alarmLevel == "2") {
                    if (_.includes(item.itemValue, ".")) {
                        // ＞下限小值，≤下限大值，
                        // ≥上限小值， ＜上限大值
                        if (cfgItem.criticalUpperObj) {
                            if ((parseFloat(item.itemValue) >= parseFloat(cfgItem.criticalUpperObj.criticalMin) && cfgItem.criticalUpperObj.criticalMin) && (parseFloat(item.itemValue) < parseFloat(cfgItem.criticalUpperObj.criticalMax)) && cfgItem.criticalUpperObj.criticalMax) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalUpperObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalUpperObj.criticalMax
                            }
                            if ((parseFloat(item.itemValue) > parseFloat(cfgItem.criticalLowerObj.criticalMin) && cfgItem.criticalLowerObj.criticalMin) && (parseFloat(item.itemValue) <= parseFloat(cfgItem.criticalLowerObj.criticalMax) && cfgItem.criticalLowerObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalLowerObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalLowerObj.criticalMax
                            }
                        }
                    } else {
                        if (cfgItem.criticalUpperObj) {
                            if ((item.itemValue >= cfgItem.criticalUpperObj.criticalMin && cfgItem.criticalUpperObj.criticalMin) && (item.itemValue < cfgItem.criticalUpperObj.criticalMax && cfgItem.criticalUpperObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalUpperObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalUpperObj.criticalMax
                            }
                            if ((item.itemValue > cfgItem.criticalLowerObj.criticalMin && cfgItem.criticalLowerObj.criticalMin) && (item.itemValue <= cfgItem.criticalLowerObj.criticalMax && cfgItem.criticalLowerObj.criticalMax)) {
                                catchCfg = _.cloneDeep(cfgItem)
                                catchCfg.criticalLower = catchCfg.criticalLowerObj.criticalMin
                                catchCfg.criticalUpper = catchCfg.criticalLowerObj.criticalMax
                            }
                        }
                    }
                } else if (cfgItem.alarmLevel == "3") {
                    // ≥上限，≤下限
                    if (_.includes(item.itemValue, ".")) {
                        if ((parseFloat(item.itemValue) >= parseFloat(cfgItem.criticalUpper) && cfgItem.criticalUpper) || (parseFloat(item.itemValue) <= parseFloat(cfgItem.criticalLower) && cfgItem.criticalLower)) {
                            catchCfg = cfgItem
                        }
                    } else {
                        if ((item.itemValue >= cfgItem.criticalUpper && cfgItem.criticalUpper) || (item.itemValue <= cfgItem.criticalLower && cfgItem.criticalLower)) {
                            catchCfg = cfgItem
                        }
                    }
                }
            })
            if (catchGroupKey != -1) {
                if (catchCfg) {
                    xindianData[catchKey].groupItems[catchGroupKey].filterAlarmCfgs = catchCfg
                }
                xindianData[catchKey].groupItems[catchGroupKey].itemValue = item.itemValue
                xindianData[catchKey].groupItems[catchGroupKey].itemDateTime = item.itemDateTime
                // 组的itemDataTime放在外面一层
                if (item.itemDateTime && item.itemDateTime != null && item.itemDateTime != 0) {
                    xindianData[catchKey].itemDateTime = item.itemDateTime
                }
            } else {
                if (catchCfg) {
                    xindianData[catchKey].filterAlarmCfgs = catchCfg
                }
                xindianData[catchKey].itemValue = item.itemValue
                xindianData[catchKey].itemDateTime = item.itemDateTime
            }
        }
    })
}

function filterGroupItems(groupAlarmCfgs) {
    _.forEach(groupAlarmCfgs, item => {
        // 找到每一个正常值范围的配置项
        // 赋给原来的alarmCfgs
        // 如果没有正常值范围，则只能给空对象
        item.filterAlarmCfgs = _.find(item.alarmCfgs, { 'alarmLevel': "0" }) || {};
    })
    return _.orderBy(groupAlarmCfgs, 'itemSubOrder', 'asc');
}

function filterSimpleItem(alarmCfgs) {
    // 如果没有正常值范围，则只能给空对象
    return _.find(alarmCfgs, { 'alarmLevel': "0" }) || {};
}

function filterRealtimeList(templateItemCfg) {
    if (_.isEmpty(templateItemCfg)) {
        return []
    }
    let realtime = []
    _.forEach(templateItemCfg.groupItems, item => {
        // 往所有所有的危急值规则里存一份
        realtime.push({
            itemName: item.groupName,
            itemSubOrder: item.itemSubOrder,
            itemUnit: item.groupAlarmCfgs[0].itemUnit,
            itemColor: item.groupAlarmCfgs[0].itemColor,
            groupItems: filterGroupItems(item.groupAlarmCfgs)
        });
    })
    _.forEach(templateItemCfg.notGroupItems, item => {
        // 往所有所有的危急值规则里存一份
        item.filterAlarmCfgs = filterSimpleItem(item.alarmCfgs);
        realtime.push(item);
    })
    return _.orderBy(realtime, 'itemSubOrder', 'asc');
}

let findMenuByRouteName = (data, routeName) => {
    let catchMenu = []
    let recursionFunc = (data, routeName) => {
        data.forEach(item => {
            if (item.name == routeName) {
                catchMenu = item
            }
            if (item.children && item.children.length > 0) {
                recursionFunc(item.children, routeName);
            }
        });
    }
    recursionFunc(data, routeName);
    return _.isEmpty(catchMenu) ? {} : catchMenu;
};

let findMenuById = (data, id) => {
    let catchMenu = []
    let recursionFunc = (data, id) => {
        data.forEach(item => {
            if (item.id == id) {
                catchMenu = item
            }
            if (item.children && item.children.length > 0) {
                recursionFunc(item.children, id);
            }
        });
    }
    recursionFunc(data, id);
    return _.isEmpty(catchMenu) ? {} : catchMenu;
};

let getLastFirstRouteName = (data) => {
    if (_.isEmpty(data)) {
        return ""
    }
    if (_.isEmpty(data[0].children)) {
        return data[0].name
    }
    if (data[0].children && data[0].children.length > 0) {
        return getLastFirstRouteName(data[0].children);
    }
};

let deleteDisableMenu = (data) => {
    if (_.isEmpty(data)) {
        return data
    }
    for (let i = 0; i < data.length; i++) {
        const obj = data[i]
        if (obj != null && obj.order == -1) {
            data.splice(i, 1)
            i = i - 1
        }
        if (obj.children && obj.children.length > 0) {
            deleteDisableMenu(obj.children);
        }
    }
    return data
};
//遍历树结构删除长度没有的children
function treeFun(data) {
    return data.map(item => {
        if (item.children.length > 0) {
            this.treeFun(item.children)
        } else {
            delete item.children
        }
        return item
    })
}
// 获取当天零点时间戳
function getZeroDate() {
    return dayjs().startOf('date').valueOf()
}
// 获取明天零点时间戳
function getZeroNextDate() {
    return dayjs().add(1, 'day').startOf('day').valueOf()
}

export default {
    dateFormat,
    sleep,
    changeUndefined,
    updateWsData,
    filterRealtimeList,
    getLastFirstRouteName,
    deleteDisableMenu,
    findMenuByRouteName,
    findMenuById,
    treeFun,
    getZeroDate,
    setDateFormat,
    getZeroNextDate
}